package cn.stylefeng.guns.sys.modular.plotHouseInfo.service;

import cn.stylefeng.guns.sys.modular.plotHouseInfo.model.TreeBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class plotTree {
    public static long treeId = 1l;

    private String id;
    private String label;
    List<HouseTree> children = new ArrayList();

    public plotTree(String id, String label) {
        super();
        this.id = id;
        this.label = label;
    }

    //加楼
    public void addHouse(TreeBean bean) {
        if(children.stream().filter(i -> bean.getBuild_no().equals(i.getLabel())).count() == 0) {
            this.children.add(new HouseTree(plotTree.treeId++ +"", bean.getBuild_no()));
        }
    }
    //加单元
    public void addUnit(TreeBean bean) {
        children.stream()
                .filter(i -> i.getLabel().equals(bean.getBuild_no()))//查楼
                .forEach(i -> i.addUnit(bean));
    }
    //加层
    public void addLevel(TreeBean bean) {
        children.stream()
                .filter(house -> house.getLabel().equals(bean.getBuild_no()))
                .forEach(house -> {
                    house.getChildren().stream()
                            .filter(unit -> unit.getLabel().equals(bean.getUnit_no()))
                            .forEach(unit -> unit.addLevel(bean))
                    ;
                });
    }
    //加户
    public void addDoor(TreeBean bean) {
        children.stream()
                .filter(house -> house.getLabel().equals(bean.getBuild_no()))
                .forEach(house -> {
                    house.getChildren().stream()
                            .filter(unit -> unit.getLabel().equals(bean.getUnit_no()))
                            .forEach(unit -> {
                                        unit.getChildren().stream()
                                                .filter(level -> level.getLabel().equals(bean.getLevel_no()))
                                                .forEach(level -> level.addDoor(bean));
                                    }
                            )
                    ;
                });
    }

    public void putFirstLevelOpen() {
//		for(HouseTree houseTree: this.children) {
//			for(UnitTree unitTree: houseTree.getChildren()) {
//				for(LevelTree levelTree: unitTree.getChildren()) {
//					if(levelTree.getChildren().size() > 0) {
//						for(Map map: levelTree.getChildren()) {
//							map.put("level", "1");
//						}
//						return;
//					}
//				}
//			}
//		}
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public List<HouseTree> getChildren() {
        return children;
    }

    public void setChildren(List<HouseTree> children) {
        this.children = children;
    }
}
